package com.yonyou.brigade.cron.order.fanscar;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.dangdang.ddframe.job.api.ShardingContext;
import com.dangdang.ddframe.job.api.simple.SimpleJob;
import com.yonyou.brigade.basic_init.enums.ChannelEnum;
import com.yonyou.brigade.basic_init.model.BriChannel;
import com.yonyou.brigade.basic_init.model.Developer;
import com.yonyou.brigade.basic_init.service.IBriChannelService;
import com.yonyou.brigade.basic_init.service.IDeveloperService;
import com.yonyou.brigade.third_parties.fanscar.model.FanscarConfig;
import com.yonyou.brigade.third_parties.fanscar.service.IFanscarOrderService;
import com.yonyou.brigade.util.operateutil.DateUtils;
import com.yonyou.ybzboot.starter.elasticjob.lite.annotation.ElasticJobConfig;
import com.yonyou.ybzboot.starter.mail.model.Email;
import com.yonyou.ybzboot.starter.mail.service.IMailService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@ElasticJobConfig(cron = "0 50 3 * * ? ", shardingTotalCount = 1)
public class FetchFanscarFlightOrders implements SimpleJob {

    Logger log= LoggerFactory.getLogger(this.getClass());

    @Autowired
    @Qualifier("briChannelServiceImpl")
    private IBriChannelService briChannelService;

    @Autowired
    private IFanscarOrderService fanscarOrderService;

    @Autowired
    @Qualifier("developerServiceImpl")
    private IDeveloperService developerService;

    @Autowired
    private IMailService mailService;

    @Value("${brigade.cur}")
    private String curEnv;

    @Override
    public void execute(ShardingContext shardingContext) {
        log.info("<<<<fanscar-flight-pull-orders-begin>>>>");
        List<BriChannel> briChannels = briChannelService.getEnableChannelByChannel(ChannelEnum.FANSCAR.getName());
        List<String> channelConfigs = new ArrayList<>();
        for (BriChannel briChannel : briChannels) {
            String channelConfig = briChannel.getConfig();
            if (channelConfigs.contains(channelConfig)) {
                continue;
            }
            channelConfigs.add(channelConfig);
            String tenant = briChannel.getTenant();
            Date begin = DateUtils.strToDateTime(DateUtils.getCurrentDateTimeMinusOneDay());
            Date end = DateUtils.strToDateTime(DateUtils.getCurrentDateTime());
            FanscarConfig fanscarConfig = (FanscarConfig) briChannelService.getChannelConfig(tenant, ChannelEnum.FANSCAR.getName(), briChannel.getOrgpk(), new FanscarConfig());
            try {
                fanscarOrderService.getFanscarFlightOrders(fanscarConfig, tenant, begin, end, briChannel.getId());
            } catch (Exception e) {
                Wrapper<Developer> developerWrapper = new EntityWrapper<>();
                developerWrapper.where("type={0}", "KF");
                List<Developer> developers = developerService.selectList(developerWrapper);
                String developerEmails = "";
                for (int j = 0; j < developers.size(); j++) {
                    developerEmails += developers.get(j).getEmail();
                    if (j < developers.size() - 1) {
                        developerEmails += ",";
                    }
                }
                Email email = new Email();
                String[] emails = developerEmails.split(",");
                email.setEmail(emails);
                email.setSubject(curEnv + "环境泛嘉机票订单拉单失败");
                email.setContent(e.getMessage());
                try {
                    log.error(e.getMessage());
                    mailService.send(email);
                } catch (Exception e1) {
                    log.error("提示" + curEnv + "环境泛嘉机票订单拉单失败的邮件发送失败！" + e1.getMessage());
                }
            }
        }
        log.info("<<<<fanscar-flight-pull-orders-end>>>>");
    }
}
